package org.example.kidscode.controller;

import org.example.kidscode.entity.CardStatistics;
import org.example.kidscode.service.CardStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@CrossOrigin
@RestController
@RequestMapping("/api/statistics")
public class CardStatisticsController {

    @Autowired
    private CardStatisticsService service;

    @GetMapping
    public List<CardStatistics> getAll() {
        return service.findAll();
    }

    @GetMapping("/{id}")
    public Optional<CardStatistics> getById(@PathVariable Long id) {
        return service.findById(id);
    }

    @GetMapping("/student/{studentId}")
    public List<CardStatistics> getByStudentId(@PathVariable String studentId) {
        return service.findByStudentId(studentId);
    }

    @PostMapping
    public CardStatistics create(@RequestBody CardStatistics statistics) {
        return service.save(statistics);
    }

    @PutMapping("/{id}")
    public CardStatistics update(@PathVariable Long id, @RequestBody CardStatistics statistics) {
        statistics.setStatId(id);
        return service.save(statistics);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        service.deleteById(id);
    }
}

